<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function login(){
    	$this->display();
    }
    public function do_login(){
        $rules = array(     
            array('verify','require','验证码必须填写！'),
            array('username','require','用户名必须填写！'),
            array('password','require','密码必须填写！'),
        );
        $User = M("user");
        if (!$User->validate($rules)->create()){    
             $this->error($User->getError());
        }else{ 
             $verify = new \Think\Verify();
             if(!$verify->check(I('post.verify'))){
                $this->error('验证码不正确！');
             }
            $User = M('user')->where(array('username'=>I('post.username'),'password'=>md5(I('post.password'))))->find();
                if(!$User){
                    $this->error('用户名密码不正确！');
                }
                $_SESSION['user_is_login'] = true;
                $_SESSION['username'] = I('post.username');
                $_SESSION['user_id'] = $User['id'];

                $this->redirect('front/index');
        }
    }
    public function verify(){
        $Verify = new \Think\Verify();
        $Verify->length   = 4;
        $Verify->entry();
    }
    public function regedit(){
        $this->display();
    }
    public function logout(){
        session_destroy();
        $this->redirect('front/index');
    }
    public function do_regedit(){
        $rules = array(     
            array('verify','require','验证码必须填写！'),
            array('username','require','用户名必须填写！'),
            array('password','require','密码必须填写！'),
            array('password_repeat','password','确认密码不正确',0,'confirm'),
        );
        $User = M("user");
        if (!$User->validate($rules)->create()){    
             $this->error($User->getError());
        }else{ 
             $verify = new \Think\Verify();
             if(!$verify->check(I('post.verify'))){
                $this->error('验证码不正确！');
             }
            $User = M('user')->where(array('username'=>I('post.username')))->find();
                if($user){
                    $this->error('用户名已存在！');
                }
                $User = M("user");
                $data = array(
                    'username' => I('post.username'),
                    'password' => md5(I('post.password')),
                    'create_time' => time()
                );
                $User->add( $data);
                $this->success('注册成功，请登录','login');
        }
    }
}